-module(unknown_packet_evt).
-behaviour(gen_event).
-export([start_link/0, init/1,handle_event/2,handle_call/2,handle_info/2,terminate/2,code_change/3]).
-define(SERVER,?MODULE).


start_link() ->
	{ok,Pid} = gen_event:start_link({local,?SERVER}),
	gen_event:add_handler(?SERVER,?MODULE,[]),
	{ok,Pid}.
init([]) ->
	io:format("register the event ~s ~n",[?SERVER]),
	{ok,dict:new()}.
handle_event({Act,{Protocol,_Index,{Source,Dest}}},Dict) ->
	Counter = try
				dict:fetch({Act,Protocol,Source,Dest},Dict)
			  catch
			    _ ->
					0
			  end,
	{ok,dict:store({Act,Protocol,Source,Dest},Counter + 1,Dict)};
handle_event(Event,State) ->
	io:format("recevied the event ~p~n",[Event]),
	{ok,State}.
handle_call(list,State) ->
	{ok,dump_counter(State),State};
handle_call(_Request,State) ->
	{ok,ok,State}.
handle_info(_Info,State) ->
	{ok,State}.
terminate(Reason,_State) ->
	io:format("terminte with ~p ~n",[Reason]), 
	ok.
code_change(_, State, _) ->
	{ok,State}.
dump_counter(Counters) ->
	dict:to_list(Counters),
	ok.
